/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_kernel.htm
 *
 * Zhiqim Kernel is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.kernel.json;

/**
 * 字符串封装类，用于{}和[]情况下不认为是字符串的特殊处理
 * 1、{}[]字符串下不加两边引号，也不作转义处理
 * 2、null/true/false/number不要引号也不要转义的
 * 3、正常字符串，toString时需要对有转义要求的加前置转义符，且两边加引号
 *
 * @version v1.5.2 @author zouzhigang 2020-5-18 新建与整理
 */
public class JsonString
{
    private String value;
    private boolean escape;
    
    public JsonString(int val)
    {
        val(val);
    }
    
    public JsonString(long val)
    {
        val(val);
    }
    
    public JsonString(boolean val)
    {
        val(val);
    }
    
    public JsonString(String val)
    {
        val(val);
    }
    
    public JsonString(String val, boolean escape)
    {
        val(val, escape);
    }
    
    public JsonString val(int val)
    {
        this.value = String.valueOf(val);
        this.escape = false;
        return this;
    }
    
    public JsonString val(long val)
    {
        this.value = String.valueOf(val);
        this.escape = false;
        return this;
    }
    
    public JsonString val(boolean val)
    {
        this.value = String.valueOf(val);
        this.escape = false;
        return this;
    }
    
    public JsonString val(String val)
    {
        this.value = val;
        this.escape = val != null && !isArray() && !isObject();
        return this;
    }
    
    public JsonString val(String val, boolean escape)
    {
        this.value = val;
        this.escape = escape;
        return this;
    }
    
    public String val()
    {
        return value;
    }
    
    public boolean escape()
    {
        return escape;
    }
    
    public boolean isObject()
    {
        return value != null && value.startsWith("[") && value.endsWith("]");
    }
    
    public boolean isArray()
    {
        return value != null && value.startsWith("{") && value.endsWith("}");
    }
    
    @Override
    public String toString()
    {
        return value;
    }
}
